package com.gtpickupapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.LightingColorFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class RealProfilePage extends Activity {

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {    
		MenuInflater inflater = getMenuInflater();    
		inflater.inflate(R.menu.mainmenu, menu);    
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {    
		// Handle item selection    
		switch (item.getItemId()) {    
		case R.id.profile:        
			profile();
			return true;    
		case R.id.logout:        
			logout();
			return true;
		case R.id.home:
			home();
			return true;
		default:        
			return super.onOptionsItemSelected(item);    
		
		}
	}
	
	public void logout(){
		//TODO will probably need to close DB connection and remove users info on logout
		Intent myintent = new Intent(RealProfilePage.this,gtpickupapp.class);
		RealProfilePage.this.startActivity(myintent);
		finish();
	}
	
	public void profile(){
		Bundle b = getIntent().getExtras();
		Bundle bundle = new Bundle();
		bundle.putString("uName", b.getString("uName"));
		Intent myintent = new Intent(RealProfilePage.this,RealProfilePage.class);
		myintent.putExtras(bundle);
		RealProfilePage.this.startActivity(myintent);
		finish();
	}
	
	public void home(){
		//TODO make this return the user to the "home" page with all of the information reset in each tab
		Bundle b = getIntent().getExtras();
		Bundle bundle = new Bundle();
		bundle.putString("uName", b.getString("uName"));
		Intent myintent = new Intent(RealProfilePage.this,MainPage.class);
		myintent.putExtras(bundle);
		RealProfilePage.this.startActivity(myintent);
		finish();
	}
	
	@Override  
	 public void onBackPressed() {  
		 //SearchEventsGroup.group.back();
		 home();
	     return;  
	 }
	
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.realprofilepage);
		ImageView image = (ImageView) findViewById(R.id.real_profile_image);
		Bundle b = getIntent().getExtras();
		TextView username = (TextView) findViewById(R.id.real_profile_username_txt);
		username.setText(b.getString("uName"));
		//populate all of the text fields by getting information from the database
		
		
		
		
		String result = "";
		InputStream is = null;
		String uname = b.getString("uName");//username.getText().toString();
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("userName" ,uname));
		//http post
		try{
				
		        HttpClient httpclient = new DefaultHttpClient();
		        HttpPost httppost = new HttpPost("http://gtplay.webatu.com/realProfile.php");
		        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		        HttpResponse response = httpclient.execute(httppost);
		        HttpEntity entity = response.getEntity();
		        is = entity.getContent();
		}catch(Exception e){
		        Log.e("log_tag", "Error in http connection "+e.toString());
		}
		
		//convert response to string
		try{
		        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
		        StringBuilder sb = new StringBuilder();
		        String line = null;
		        while ((line = reader.readLine()) != null) {
		                sb.append(line + "\n");
		        }
		        is.close();
		 
		        result=sb.toString();
		}catch(Exception e){
		        Log.e("log_tag", "Error converting result "+e.toString());
		}
		 
		//parse json data
		try{
				//
		        JSONArray jArray = new JSONArray(result);
		        TextView user = (TextView)findViewById(R.id.real_profile_username_txt);
        		TextView name = (TextView)findViewById(R.id.real_profile_name_txt);
        		TextView sex = (TextView)findViewById(R.id.real_profile_sex_txt);
        		TextView weight = (TextView)findViewById(R.id.real_profile_weight_txt);
        		TextView age = (TextView)findViewById(R.id.real_profile_age_txt);
        		TextView bench = (TextView)findViewById(R.id.real_profile_bench_txt);
        		TextView squat = (TextView)findViewById(R.id.real_profile_squat_txt);
        		TextView pi = (TextView)findViewById(R.id.real_profile_pi_txt);
        		JSONObject json_data = new JSONObject();
		        for(int i=0;i<jArray.length();i++){
		        	    json_data = jArray.getJSONObject(i);
		        }
		        
		        user.setText(json_data.getString("User_Name"));
		        String n = json_data.getString("First_Name") + " " + json_data.getString("Last_Name");
		        name.setText(n);
		        sex.setText(json_data.getString("Sex"));
                age.setText(Integer.toString(json_data.getInt("Age")));
                bench.setText(Integer.toString(json_data.getInt("Bench_Max")));
                weight.setText(Integer.toString(json_data.getInt("Weight")));
                squat.setText(Integer.toString(json_data.getInt("Squat_Max")));
                pi.setText(Integer.toString(json_data.getInt("Pi_Mile_Time")));
		        
		}
		catch(JSONException e){
		        Log.e("log_tag", "Error parsing data "+e.toString());
		}
		
		
		
		
		
		
		final Button edit = (Button) findViewById(R.id.real_profile_edit);
        edit.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xC6930A));
        edit.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		
        		Bundle bundle = new Bundle();
        		//get all of the text field values to pass to the edit profile page
        		TextView username = (TextView)findViewById(R.id.real_profile_username_txt);
        		TextView name = (TextView)findViewById(R.id.real_profile_name_txt);
        		TextView sex = (TextView)findViewById(R.id.real_profile_sex_txt);
        		TextView weight = (TextView)findViewById(R.id.real_profile_weight_txt);
        		TextView age = (TextView)findViewById(R.id.real_profile_age_txt);
        		TextView bench = (TextView)findViewById(R.id.real_profile_bench_txt);
        		TextView squat = (TextView)findViewById(R.id.real_profile_squat_txt);
        		TextView pi = (TextView)findViewById(R.id.real_profile_pi_txt);
        		
        		bundle.putString("uName", username.getText().toString());
        		bundle.putString("name", name.getText().toString());
        		bundle.putString("sex", sex.getText().toString());
        		bundle.putString("weight", weight.getText().toString());
        		bundle.putString("age", age.getText().toString());
        		bundle.putString("bench", bench.getText().toString());
        		bundle.putString("squat", squat.getText().toString());
        		bundle.putString("pi", pi.getText().toString());
        		
        		
        		
        		Intent myintent = new Intent(RealProfilePage.this, ProfilePage.class);
        		myintent.putExtras(bundle);
        		RealProfilePage.this.startActivity(myintent);
        		finish();
        		
        	}         
        });
	}
}
